<template>
	<view>
		<view>
			<u-tabs style="width: 90%;margin:0 5%;" :list="listTabs" @click='tabsClick' lineWidth="40" lineHeight="6"
				lineColor="#1590EF" :activeStyle="{
		    fontWeight: 'bold',
		    transform: 'scale(1.05)'
		}" :inactiveStyle="{
			 color: '#000',
		}"></u-tabs>
		</view>
		<view class="inspection_details">
			<!-- <u-upload v-if="activeName=='企业上传材料'" :fileList="fileList" accept="file" @afterRead="afterRead"
				@delete="deletePic" name="3" multiple width='150' height='150'></u-upload> -->
			<view v-if="activeName=='企业上传材料'">
				<view v-for="item in materialsFileList(options)">
					<!-- <u-link v-if="item.url" :href="item.url" :text="'《'+item.name+'》'" color="#007BFF"
						style='margin: 15px 0;' fontSize='30' @click="click">
					</u-link> -->
					<text v-if="item.url" @click="click(item)"
						style='display: block;margin: 10px 0;color: #007BFF;'>《{{item.name}}》</text>
					<text v-else>《{{item.name}}》</text>
				</view>
			</view>
			<view v-else>
				<text>{{textData}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				listTabs: [{
						name: '企业上传材料',
					}, {
						name: '检查标准',
					},
					{
						name: '检查方法',
					},
					{
						name: '检查依据',
					}, {
						name: '处罚依据',
					},
				],
				// fileList: [],
				activeName: "企业上传材料",
				options: {},
				textData: ""
			}
		},
		onLoad(option) {
			this.options = JSON.parse(option.moreData)
			this.tabsClick({
				name: '企业上传材料',
				index: 0,
			})
		},
		mounted() {},
		methods: {
			tabsClick(item) {
				this.activeName = item.name
				if (item.index == 1) {
					this.textData = this.options.inspStandard
				}
				if (item.index == 2) {
					this.textData = this.options.inspMethod
				}
				if (item.index == 3) {
					this.textData = this.options.punishmentBasis
				}
				if (item.index == 4) {
					this.textData = this.options.enforcementBasis
				}
			},

			click(item) {
				let platform = uni.getSystemInfoSync().platform;
				if (platform == 'ios') {
					this.fDownLoadFileIos(item.url)
				} else if (platform == 'android') {
					this.fDownLoadFileAndroid(item)
				}
			},

			// ios下载
			fDownLoadFileIos(url) {
				uni.downloadFile({
					url: url,
					success: (res) => {
						if (res.statusCode === 200) {
							// console.log('下载成功', res);
							uni.saveFile({
								tempFilePath: res.tempFilePath,
								success: (res) => {
									// console.log('保存成功', res);
									var savedFilePath = res.savedFilePath;
									this.fGetSavedFileList();
								}
							});
						}
					}
				});
			},

			fGetSavedFileList() {
				this.aFileList = [];
				uni.getSavedFileList({
					success: (res) => {
						// console.log(res);
						// console.log(res.fileList, '已下载的文件列表');
						this.fOpenFileIos(res.fileList)

					}
				});
			},

			fOpenFileIos(oData) {
				//上面的获取本地的已下载文件列表中的对象
				uni.openDocument({
					filePath: oData.filePath,
					showMenu: true,
					success: (res) => {
						console.log(res, '打开文档成功')
					},
					fail: (res) => {
						console.log(res, '打开文档失败')
					}
				});
			},

			// 安卓下载文件
			fDownLoadFileAndroid(item) {
				var url = item.url
				let dtask = plus.downloader.createDownload(url, {
						/**
						 *  本地路径开头使用file://，跟上手机文件本地目录storage/emulated/0，就是用户文件管理器能看到的了，
						 *  后缀是用于文件命名和格式修改
						 *  file://storage/emulated/0/后面就是用户文件管理的第一层，可以创建目录，
						 * */
						filename: "file://storage/emulated/0/supervise/" + item.name + ".docx"
					},
					(d, status) => {
						//d为下载的文件对象
						if (status == 200) {
							// console.log(d, '下载成功', status);
							//下载成功,d.filename是文件在保存在本地的相对路径，使用下面的API可转为平台绝对路径
							let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
							// plus.runtime.openFile(d.filename); //选择软件打开文件
							this.fGetSavedFileList();
						} else {
							//下载失败
							plus.downloader.clear(); //清除下载任务
						}
					})
				dtask.start();
			},


			// 获取本地的已下载文件列表
			fGetSavedFileList() {
				this.aFileList = [];
				plus.io.resolveLocalFileSystemURL(
					"file://storage/emulated/0/supervise/", //指定的目录
					(entry) => {
						var directoryReader = entry.createReader(); //获取读取目录对象
						directoryReader.readEntries(
							(entries) => { //返回的是指定文件夹下的文件列表和                                    uni.getSavedFileList(OBJECT)效果一样
								// console.log(entries)
								this.aFileList = entries
								this.fOpenFileAndroid(entries)
							},
							(err) => {
								// console.log("访问目录失败");
							});
					},
					(err) => {
						// console.log("访问指定目录失败:" + err.message);
					});



			},

			// 安卓打开文档
			fOpenFileAndroid(oFile) {
				//oFile是上面获取本地的已下载文件列表中的对象
				// console.log(oFile)
				plus.runtime.openFile(oFile.fullPath); //打开文件
			},




			// 筛选
			materialsFileList(data) {
				if (!data || !data.coMaterials) {
					return []
				}
				return data.coMaterials.split(',').map(name => {
					const index = data.uploaded.findIndex(item => item.name === name)
					if (index == -1) {
						return {
							id: '',
							url: '',
							name
						}
					} else {
						return data.uploaded[index]
					}
				});
			}


		},
	}
</script>

<style lang="scss" scoped>
	.inspection_details {
		padding: 40rpx;
	}
</style>
